package com.goldenautumn.prefabricate.business.order

import com.goldenautumn.prefabricate.business.entity.BasicList
import com.goldenautumn.prefabricate.business.entity.BasicPage
import com.goldenautumn.prefabricate.business.entity.CarPreviewInfo
import com.goldenautumn.prefabricate.business.entity.ExpressResp
import com.goldenautumn.prefabricate.business.entity.OrderDetailsReq
import com.goldenautumn.prefabricate.business.entity.OrderInfoVo
import com.goldenautumn.prefabricate.business.entity.OrderListReqParams
import com.goldenautumn.prefabricate.business.entity.OrderListResp
import com.goldenautumn.prefabricate.business.entity.RefundOrderDetailsReq
import com.goldenautumn.prefabricate.business.entity.RefundOrderInfoVo
import com.goldenautumn.prefabricate.business.service.OrderService
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_COMPLETE
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_PENDING
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_RECEIVING
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_SHIPMENT
import com.goldenautumn.prefabricate.utils.getToken
import qncx.external.common.network.SimpleRepository

class OrderRepositoryImpl : SimpleRepository() {

    suspend fun getOrderList(source: OrderListReqParams): OrderListResp<OrderInfoVo> {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.order/lists"
        map["token"] = getToken()
        map["dataType"] = getOrderType(source.status)
        return getService(OrderService::class.java).getOrderList(map)
    }

    suspend fun onCreateOrder(source: OrderListReqParams): OrderListResp<OrderInfoVo> {
        var map = HashMap<String, Any>()
        map["s"] = "api/order/buyNow"
        map["token"] = getToken()
        return getService(OrderService::class.java).getOrderList(map)
    }

    suspend fun onCarPreview(carIDs: String,isPost:Boolean,payType:String? = null): CarPreviewInfo {
        var map = HashMap<String, Any>()
        map["s"] = "api/order/cart"
        map["token"] = getToken()
        map["cart_ids"] = carIDs
        if(isPost && payType != null) {
            var type = payType.toFloat().toInt()
            map["pay_type"] = "$type"
        }
        return getService(OrderService::class.java).onCarPreview(map)
    }

    suspend fun onCarPreviewPost(carIDs: String,isPost:Boolean,payType:String? = null): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/order/cart"
        map["token"] = getToken()
        map["cart_ids"] = carIDs
        if(isPost && payType != null) {
            var type = payType.toFloat().toInt()
            map["pay_type"] = "$type"
        }
        return getService(OrderService::class.java).onCarPreviewPOST(map)
    }

    suspend fun onOrderCanCel(orderSn: String): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.order/cancel"
        map["token"] = getToken()
        map["order_id"] = orderSn
        return getService(OrderService::class.java).onOrderCanCel(map)
    }

    suspend fun onOrderDetails(orderSn: String): OrderDetailsReq {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.order/detail"
        map["token"] = getToken()
        map["order_id"] = orderSn
        return getService(OrderService::class.java).onOrderDetails(map)
    }

    suspend fun onOrderPay(orderSn: String,payM:String): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.order/pay"
        var map1 = HashMap<String, Any>()
        map1["token"] = getToken()
        map1["order_id"] = orderSn
        map1["payType"] = payM
        return getService(OrderService::class.java).onOrderPay(map,map1)
    }

    suspend fun onOrderReceiving(orderSn: String): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.order/receipt"
        map["token"] = getToken()
        map["order_id"] = orderSn
        return getService(OrderService::class.java).onOrderReceiving(map)
    }

    suspend fun onExpressInfo(orderSn: String): ExpressResp {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.order/express"
        map["token"] = getToken()
        map["order_id"] = orderSn
        return getService(OrderService::class.java).expressInfo(map)
    }

    suspend fun onOrderRefund(type: String,goodsID: String,content: String): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.refund/apply"
        map["token"] = getToken()

        var map1 = HashMap<String, Any>()
        map1["type"] = type
        map1["content"] = content
        map1["order_goods_id"] = goodsID
        return getService(OrderService::class.java).orderRefund(map,map1)
    }

    suspend fun onRefundList(type: String): BasicPage<RefundOrderInfoVo> {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.refund/lists"
        map["token"] = getToken()
        map["state"] = type
        return getService(OrderService::class.java).refundList(map)
    }

    suspend fun onRefundDetails(type: String): RefundOrderDetailsReq {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.refund/detail"
        map["token"] = getToken()
        map["order_refund_id"] = type
        return getService(OrderService::class.java).refundDetails(map)
    }

    fun getOrderType(status:String):String {
        var type = when(status) {
            ORDER_TYPE_PENDING -> "payment"
            ORDER_TYPE_SHIPMENT -> "delivery"
            ORDER_TYPE_RECEIVING -> "received"
            ORDER_TYPE_COMPLETE -> "comment"
            else -> "all"
        }
        return type
    }


}